import api from './auth'

// 角色管理API
export const roleAPI = {
  // 获取所有角色
  getRoles: () => {
    return api.get('/role')
  },

  // 根据ID获取角色
  getRoleById: (id) => {
    return api.get(`/role/${id}`)
  },

  // 添加角色
  addRole: (data) => {
    return api.post('/role', {
      roleName: data.roleName,
      isActived: data.isActived,
      dataRange: data.dataRange || 'All',
      remarks: data.remarks
    })
  },

  // 更新角色
  updateRole: (id, data) => {
    return api.post(`/role/${id}`, {
      roleName: data.roleName,
      isActived: data.isActived,
      dataRange: data.dataRange,
      remarks: data.remarks || data.Remarks
    })
  },

  // 删除角色
  deleteRole: (id) => {
    return api.delete(`/role/${id}`)
  },

  // 获取角色权限
  getRolePermissions: (roleId) => {
    return api.get(`/permissionrole/${roleId}`)
  },

  // 分配权限给角色
  assignPermissions: (data) => {
    return api.post('/permissionrole', {
      roleId: data.roleId,
      permissionId: data.permissionId
    })
  },

  // 移除角色权限
  removeRolePermissions: (roleId) => {
    return api.delete(`/permissionrole/${roleId}`)
  },

  // 移除角色的指定权限
  removeRolePermission: ({ roleId, permissionId }) => {
    return api.delete('/permissionrole', { params: { roleId, permissionId } })
  }
}

export default roleAPI
